package com.iscale.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.iscale.model.dto.Pager;
import com.iscale.model.entity.SysUser;
import com.iscale.service.SysUserService;
import com.iscale.util.ResponseCode;
import com.iscale.util.Result;
import com.iscale.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Andychen
 * @since 2017-03-01
 */
@Api(value = "用户管理模块",description = "用户管理模块")
@Controller
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(SysUserController.class);
    @Autowired
    SysUserService userService;


    /**
     * 新增用户
     *
     * @param loginName     登录名
     * @param zhName        中文名
     * @param enName        英文名
     * @param sex           性别
     * @param birth         生日
     * @param email         邮箱
     * @param phone         电话
     * @param address       地址
     * @param password      密码
     * @param isFinal       是否可修改
     */
    @ApiOperation(value = "新增用户", httpMethod = "POST", produces = "application/json", response = Result.class)
//    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public Result insert(@ApiParam(value = "登录名", required = true) @RequestParam String loginName,
                         @RequestParam String zhName,
                         @RequestParam(defaultValue = "", required = false) String enName,
                         @RequestParam int sex,
                         @RequestParam(defaultValue = "", required = false) String birth,
                         @RequestParam(defaultValue = "", required = false) String email,
                         @RequestParam(defaultValue = "", required = false) String phone,
                         @RequestParam(defaultValue = "", required = false) String address,
                         @RequestParam String password,
                         @RequestParam(defaultValue = "1") int isFinal) {
//        boolean isExistLoginName = sysUserService.isExistLoginName(loginName);
//        if (isExistLoginName) {
//            return Result.error(ResponseCode.name_already_exist.getMsg());
//        }
        if ((!StringUtils.hasText(password)) && password.length() < 6) {
            return Result.error("请设置密码长度大于等于6");
        }
        SysUser user = new SysUser();
        user.setLoginName(loginName);
        user.setZhName(zhName);
        user.setEnName(enName);
        user.setSex(sex);
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setBirth(birth);
        user.setIsFinal(isFinal);
        String salt = UUID.randomUUID().toString().replaceAll("-", "");
        user.setPasswordSalt(salt);
        user.setPassword(StringUtil.createPassword(password, salt, 2));

        boolean result = userService.insert(user);
        if(result){
            return Result.success(user.getId());
        }else{
            return Result.error(ResponseCode.error);
        }
    }


    @ApiOperation(value = "分页查询用户", httpMethod = "POST", produces = "application/json", response = Result.class)
//    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectUserByPager", method = RequestMethod.POST)
    public Result selectUserByPager(@ApiParam(value = "当前页", required = false) @RequestParam(defaultValue = "1", required = false) int page,
                                    @ApiParam(value = "每页显示数", required = false) @RequestParam(defaultValue = "10", required = false) int size) {
        Pager<SysUser> pages = userService.selectUserByPage(page,size,"create_time",true);

        return Result.success(pages);
    }

    @ApiOperation(value = "根据ID查询用户", httpMethod = "POST", produces = "application/json", response = Result.class)
//    @RequiresPermissions("user:insert")
    @ResponseBody
    @RequestMapping(value = "selectUserById", method = RequestMethod.POST)
    public Result selectUserById(@ApiParam(value = "用户id", required = true) @RequestParam(defaultValue = "0", required = true) Long userId) {
         SysUser user = userService.selectById(userId);
        return Result.success(user);
    }

    @ApiOperation(value = "设置session", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "setSession", method = RequestMethod.POST)
    public Result setSession(HttpServletRequest req) {
        HttpSession session = req.getSession(true);
        log.debug(session.getId());
        session.setAttribute("userinfo","andy");
        return Result.success("");
    }

    @ApiOperation(value = "获取session", httpMethod = "POST", produces = "application/json", response = Result.class)
    @ResponseBody
    @RequestMapping(value = "getSession", method = RequestMethod.POST)
    public Result getSession(HttpServletRequest req) {
        HttpSession session = req.getSession(true);
        log.debug(session.getId());

        log.debug(session.getAttribute("userinfo").toString());
        return Result.success("");
    }
}
